package com.autoboot.controller;

import com.autoboot.domain.bo.queryBo;
import com.autoboot.domain.vo.R;
import com.autoboot.service.DynamicTableService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户控制器示例
 * @author Maosw
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    /**
     * 数据库用户表名
     */
    private static final String TABLE_NAME = "test_user";

    /**
     * 数据库表主键ID字段名称
     */
    private static final String ID_FIELD = "id";


    @Autowired
    private DynamicTableService dynamicTableService;

    /**
     * 获取用户列表
     */
    @GetMapping
    public R<List<Map<String, Object>>> list() {
        return R.ok(dynamicTableService.listFromTable(TABLE_NAME));
    }

    /**
     * 根据ID获取用户
     */
    @GetMapping("/{id}")
    public R<Map<String, Object>> getById(@PathVariable String id) {
        return R.ok(dynamicTableService.getByIdFromTable(TABLE_NAME, ID_FIELD, id));
    }

    /**
     * 条件查询用户
     */
    @PostMapping("/query")
    public R<List<Map<String, Object>>> search(@RequestBody queryBo queryBo) {
        // 使用增强的QueryWrapper处理复杂条件
        return R.ok(dynamicTableService.listByConditionFromTable(
                TABLE_NAME,
                queryBo.getParams(),
                queryBo.getLikes(),
                queryBo.getGt(),
                queryBo.getGe(),
                queryBo.getLt(),
                queryBo.getLe(),
                queryBo.getBetween(),
                queryBo.getOrders()));
    }

    /**
     * 分页查询用户
     */
    @PostMapping("/page")
    public R<Page<Map<String, Object>>> page(@RequestBody queryBo queryBo) {
        // 将String类型的分页参数转为long
        long current = queryBo.getPageIndex() != null ? Long.parseLong(queryBo.getPageIndex()) : 1;
        long size = queryBo.getPageSize() != null ? Long.parseLong(queryBo.getPageSize()) : 10;

        // 使用增强的QueryWrapper处理复杂条件
        return R.ok(dynamicTableService.pageFromTable(
                TABLE_NAME,
                current,
                size,
                queryBo.getParams(),
                queryBo.getLikes(),
                queryBo.getGt(),
                queryBo.getGe(),
                queryBo.getLt(),
                queryBo.getLe(),
                queryBo.getBetween(),
                queryBo.getOrders()));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public R<Boolean> save(@RequestBody Map<String, Object> map) {
        return R.ok(dynamicTableService.saveToTable(TABLE_NAME, map));
    }

    /**
     * 更新用户
     */
    @PutMapping("/{id}")
    public R<Boolean> update(@PathVariable String id, @RequestBody Map<String, Object> map) {
        map.put(ID_FIELD, id);
        return R.ok(dynamicTableService.updateToTable(TABLE_NAME, ID_FIELD, map));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable String id) {
        return R.ok(dynamicTableService.removeFromTable(TABLE_NAME, ID_FIELD, id));
    }
}
